<?php namespace App\Services;

use App\Models\Item;
use Illuminate\Http\Request;
use Exception;
use Auth;
use File;

class ItemService
{

    public function getList(Request $request, $user_id = null)
    {
        $params = [];
        if ($request->input('p_catalog_id'))
            $params['p_catalog_id'] = $request->input('p_catalog_id');
        if ($request->input('catalog_id'))
            $params['catalog_id'] = $request->input('catalog_id');
        if ($request->input('province_id'))
            $params['province_id'] = $request->input('province_id');
        if ($request->input('city_id'))
            $params['city_id'] = $request->input('city_id');
        if ($request->input('district_id'))
            $params['district_id'] = $request->input('district_id');
        if ($request->input('type'))
            $params['type'] = $request->input('type');
        if ($user_id)
            $params['user_id'] = $user_id;
        $search = Item::where($params);
        if (($request->input('name')))
            $search->where('name', 'like', '%' . $request->input('name') . '%');
        return $search->orderBy($request->input('orderField', 'id'), $request->input('orderDirection', 'desc'))
            ->paginate($request->input('pageSize', config('view')['numPerPage']));
    }


    public function create(Array $data)
    {
        $itemImageService = new ItemImageService();
        $params = [
            'user_id' => Auth::user()->id,
            'name' => $data['name'],
            'describe' => $data['describe'],
            'type' => $data['type'],
            'p_catalog_id' => $data['p_catalog_id'],
            'catalog_id' => $data['catalog_id'],
            'province_id' => $data['province_id'],
            'city_id' => $data['city_id'],
            'district_id' => $data['district_id'],
            'place' => $data['place'],
            'wanted_fee' => $data['wanted_fee']
        ];
        $str = $data['type'] == 'find' ? 'find_time' : 'lost_time';
        $params[$str] = $data['time'];
        $item = Item::create($params);
        $itemImageService->saveImages($data['item_images'], $item->id);
        return $item;
    }

    public function update($id, Array $data)
    {
        $item = Item::find($id);
        $item->name = $data['name'];
        $item->name = $data['describe'];
        $item->name = $data['type'];
        $item->name = $data['p_catalog_id'];
        $item->name = $data['catalog_id'];
        $item->name = $data['province_id'];
        $item->name = $data['city_id'];
        $item->name = $data['district_id'];
        $item->name = $data['place'];
        $item->name = $data['wanted_fee'];
        $item->name = $data['lost_time'];
        $item->name = $data['find_time'];
        return $item->save();
    }

    public function delete($id)
    {
        $item = Item::find($id);
        if ($item->match) {
            throw new Exception('已匹配物品，不能删除');
        }
        foreach ($item->images as $image) {
            $file = $image->file_path . DIRECTORY_SEPARATOR . $image->file_name;
            if(File::exists($file)){
                File::delete($file);
            }
            $image->delete();
        }
        return $item->delete();
    }
}